//
//  UIButton+YCTools.m
//  Pods
//
//  Created by Apple on 2022/11/1.
//

#import "UIButton+YCTools.h"
#import "UIImage+YCTools.h"
#import <objc/runtime.h>

@implementation UIButton (YCTools)

+ (UIButton *)buttonWithFrame:(CGRect)frame
                        image:(UIImage *)image
                       target:(id)target
                       action:(SEL)seltor {
    UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
    btn.frame = frame;
    [btn setImage:image forState:UIControlStateNormal];
    [btn addTarget:target action:seltor forControlEvents:UIControlEventTouchUpInside];
    return btn;
}

+ (UIButton *)buttonWithFrame:(CGRect)frame
                        image:(UIImage *)image
                   otherState:(UIControlState)otherState
                   otherImage:(UIImage *)otherImage
                       target:(id)target
                       action:(SEL)seltor {
    UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
    btn.frame = frame;
    [btn setImage:image forState:UIControlStateNormal];
    [btn setImage:otherImage forState:otherState];
    [btn addTarget:target action:seltor forControlEvents:UIControlEventTouchUpInside];
    return btn;
}

+ (UIButton *)buttonWithFrame:(CGRect)frame
                        title:(NSString *)title
                   titleColor:(UIColor *)titleColor
                         font:(UIFont *)font
                       target:(id)target
                       action:(SEL)seltor {
    return [UIButton buttonWithFrame:frame title:title titleColor:titleColor font:font bgColor:UIColor.clearColor target:target action:seltor];
}

+ (UIButton *)buttonWithFrame:(CGRect)frame
                        title:(NSString *)title
                   titleColor:(UIColor *)titleColor
                         font:(UIFont *)font
                      bgColor:(UIColor *)bgColor
                       target:(id)target
                       action:(SEL)seltor {
    UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
    btn.frame = frame;
    [btn setTitle:title forState:UIControlStateNormal];
    [btn setTitleColor:titleColor forState:UIControlStateNormal];
    btn.titleLabel.font = font;
    btn.backgroundColor = bgColor;
    [btn addTarget:target action:seltor forControlEvents:UIControlEventTouchUpInside];
    return btn;
}

+ (UIButton *)buttonWithFrame:(CGRect)frame
                        title:(NSString *)title
                   titleColor:(UIColor *)titleColor
                         font:(UIFont *)font
                        image:(UIImage *)image
                       target:(id)target
                       action:(SEL)seltor {
    UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
    btn.frame = frame;
    [btn setTitle:title forState:UIControlStateNormal];
    [btn setTitleColor:titleColor forState:UIControlStateNormal];
    btn.titleLabel.font = font;
    [btn setImage:image forState:UIControlStateNormal];
    [btn addTarget:target action:seltor forControlEvents:UIControlEventTouchUpInside];
    return btn;
}

+ (UIButton *)buttonWithFrame:(CGRect)frame
                        title:(NSString *)title
                   titleColor:(UIColor *)titleColor
                         font:(UIFont *)font
                   otherState:(UIControlState)otherState
                   otherTitle:(nullable NSString *)otherTitler
                       target:(id)target
                       action:(SEL)seltor {
    return [UIButton buttonWithFrame:frame title:title titleColor:titleColor font:font bgColor:UIColor.clearColor otherState:otherState otherTitle:otherTitler otherTitleColor:titleColor otherBgColor:UIColor.clearColor target:target action:seltor];
}

+ (UIButton *)buttonWithFrame:(CGRect)frame
                        title:(NSString *)title
                   titleColor:(UIColor *)titleColor
                         font:(UIFont *)font
                   otherState:(UIControlState)otherState
              otherTitleColor:(UIColor *)otherTitleColor
                       target:(id)target
                       action:(SEL)seltor {
    return [UIButton buttonWithFrame:frame title:title titleColor:titleColor font:font bgColor:UIColor.clearColor otherState:otherState otherTitle:title otherTitleColor:otherTitleColor otherBgColor:UIColor.clearColor target:target action:seltor];
}

+ (UIButton *)buttonWithFrame:(CGRect)frame
                        title:(NSString *)title
                   titleColor:(UIColor *)titleColor
                         font:(UIFont *)font
                      bgColor:(UIColor *)bgColor
                   otherState:(UIControlState)otherState
                 otherBgColor:(UIColor *)otherBgColor
                       target:(id)target
                       action:(SEL)seltor {
    return [UIButton buttonWithFrame:frame title:title titleColor:titleColor font:font bgColor:bgColor otherState:otherState otherTitle:title otherTitleColor:titleColor otherBgColor:otherBgColor target:target action:seltor];
}

+ (UIButton *)buttonWithFrame:(CGRect)frame
                        title:(NSString *)title
                   titleColor:(UIColor *)titleColor
                         font:(UIFont *)font
                      bgColor:(UIColor *)bgColor
                   otherState:(UIControlState)otherState
              otherTitleColor:(UIColor *)otherTitleColor
                       target:(id)target
                       action:(SEL)seltor {
    return [UIButton buttonWithFrame:frame title:title titleColor:titleColor font:font bgColor:bgColor otherState:otherState otherTitle:title otherTitleColor:otherTitleColor otherBgColor:bgColor target:target action:seltor];
}

+ (UIButton *)buttonWithFrame:(CGRect)frame
                        title:(NSString *)title
                   titleColor:(UIColor *)titleColor
                         font:(UIFont *)font
                      bgColor:(UIColor *)bgColor
                   otherState:(UIControlState)otherState
                   otherTitle:(nullable NSString *)otherTitler
                       target:(id)target
                       action:(SEL)seltor {
    return [UIButton buttonWithFrame:frame title:title titleColor:titleColor font:font bgColor:bgColor otherState:otherState otherTitle:otherTitler otherTitleColor:titleColor otherBgColor:bgColor target:target action:seltor];
}

+ (UIButton *)buttonWithFrame:(CGRect)frame
                        title:(NSString *)title
                   titleColor:(UIColor *)titleColor
                         font:(UIFont *)font
                      bgColor:(UIColor *)bgColor
                   otherState:(UIControlState)otherState
              otherTitleColor:(UIColor *)otherTitleColor
                 otherBgColor:(UIColor *)otherBgColor
                       target:(id)target
                       action:(SEL)seltor {
    return [UIButton buttonWithFrame:frame title:title titleColor:titleColor font:font bgColor:bgColor otherState:otherState otherTitle:title otherTitleColor:otherTitleColor otherBgColor:otherBgColor target:target action:seltor];
}

+ (UIButton *)buttonWithFrame:(CGRect)frame
                        title:(NSString *)title
                   titleColor:(UIColor *)titleColor
                         font:(UIFont *)font
                      bgColor:(UIColor *)bgColor
                   otherState:(UIControlState)otherState
                   otherTitle:(nullable NSString *)otherTitle
                 otherBgColor:(UIColor *)otherBgColor
                       target:(id)target
                       action:(SEL)seltor {
    return [UIButton buttonWithFrame:frame title:title titleColor:titleColor font:font bgColor:bgColor otherState:otherState otherTitle:otherTitle otherTitleColor:titleColor otherBgColor:otherBgColor target:target action:seltor];
}

+ (UIButton *)buttonWithFrame:(CGRect)frame
                        title:(NSString *)title
                   titleColor:(UIColor *)titleColor
                         font:(UIFont *)font
                      bgColor:(UIColor *)bgColor
                   otherState:(UIControlState)otherState
                   otherTitle:(nullable NSString *)otherTitle
              otherTitleColor:(UIColor *)otherTitleColor
                 otherBgColor:(UIColor *)otherBgColor
                       target:(id)target
                       action:(SEL)seltor {
    UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
    btn.frame = frame;
    [btn setTitle:title forState:UIControlStateNormal];
    [btn setTitleColor:titleColor forState:UIControlStateNormal];
    btn.titleLabel.font = font;
    [btn setBackgroundColor:bgColor forState:UIControlStateNormal];
    
    if (otherTitle) {
        [btn setTitle:otherTitle forState:otherState];
    }
    
    [btn setTitleColor:otherTitleColor forState:otherState];
    [btn setBackgroundColor:otherBgColor forState:otherState];
    
    [btn addTarget:target action:seltor forControlEvents:UIControlEventTouchUpInside];
    
    return btn;
}

+ (UIButton *)buttonWithOnlyText:(NSString *)title
                      titleColor:(UIColor *)titleColor
                            font:(UIFont *)font
                          target:(id)target
                          action:(SEL)seltor {
    UIButton *btn = [UIButton buttonWithFrame:CGRectZero title:title titleColor:titleColor font:font bgColor:UIColor.clearColor target:target action:seltor];
    return btn;
}

- (void)setBackgroundColor:(UIColor *)backgroundColor forState:(UIControlState)state {
    [self setBackgroundImage:[UIImage imageWithColor:backgroundColor] forState:state];
}

- (CGSize)setButtonImagePosition:(YCButtonImagePosition)position spacing:(CGFloat)spacing {
    CGFloat imageWidth = self.currentImage.size.width;
            CGFloat imageHeight = self.currentImage.size.height;
        #pragma clang diagnostic push
        #pragma clang diagnostic ignored "-Wdeprecated-declarations"
            // Single line, no wrapping. Truncation based on the NSLineBreakMode.
            CGSize size = [self.currentTitle sizeWithFont:self.titleLabel.font];
            CGFloat labelWidth = size.width;
            CGFloat labelHeight = size.height;
        #pragma clang diagnostic pop
            
            CGFloat imageOffsetX = (imageWidth + labelWidth) / 2 - imageWidth / 2;//image中心移动的x距离
            CGFloat imageOffsetY = imageHeight / 2 + spacing / 2;//image中心移动的y距离
            CGFloat labelOffsetX = (imageWidth + labelWidth / 2) - (imageWidth + labelWidth) / 2;//label中心移动的x距离
            CGFloat labelOffsetY = labelHeight / 2 + spacing / 2;//label中心移动的y距离
            
            CGFloat tempWidth = MAX(labelWidth, imageWidth);
            CGFloat changedWidth = labelWidth + imageWidth - tempWidth;
            CGFloat tempHeight = MAX(labelHeight, imageHeight);
            CGFloat changedHeight = labelHeight + imageHeight + spacing - tempHeight;
            
            UIEdgeInsets imageEdgeInsets = UIEdgeInsetsZero;
            UIEdgeInsets titleEdgeInsets = UIEdgeInsetsZero;
            UIEdgeInsets contentEdgeInsets = UIEdgeInsetsZero;
            
            switch (position) {
                case YCButtonImagePositionLeft:
                    imageEdgeInsets = UIEdgeInsetsMake(0, -spacing/2, 0, spacing/2);
                    titleEdgeInsets = UIEdgeInsetsMake(0, spacing/2, 0, -spacing/2);
                    contentEdgeInsets = UIEdgeInsetsMake(0, spacing/2, 0, spacing/2);
                    break;
                    
                case YCButtonImagePositionRight:
                    imageEdgeInsets = UIEdgeInsetsMake(0, labelWidth + spacing/2, 0, -(labelWidth + spacing/2));
                    titleEdgeInsets = UIEdgeInsetsMake(0, -(imageWidth + spacing/2), 0, imageWidth + spacing/2);
                    contentEdgeInsets = UIEdgeInsetsMake(0, spacing/2, 0, spacing/2);
                    break;
                    
                case YCButtonImagePositionTop:
                    imageEdgeInsets = UIEdgeInsetsMake(-imageOffsetY, imageOffsetX, imageOffsetY, -imageOffsetX);
                    titleEdgeInsets = UIEdgeInsetsMake(labelOffsetY, -labelOffsetX, -labelOffsetY, labelOffsetX);
                    contentEdgeInsets = UIEdgeInsetsMake(imageOffsetY, -changedWidth/2, changedHeight-imageOffsetY, -changedWidth/2);
                    break;
                    
                case YCButtonImagePositionBottom:
                    imageEdgeInsets = UIEdgeInsetsMake(imageOffsetY, imageOffsetX, -imageOffsetY, -imageOffsetX);
                    titleEdgeInsets = UIEdgeInsetsMake(-labelOffsetY, -labelOffsetX, labelOffsetY, labelOffsetX);
                    contentEdgeInsets = UIEdgeInsetsMake(changedHeight-imageOffsetY, -changedWidth/2, imageOffsetY, -changedWidth/2);
                    break;
                default:
                    break;
            }
            
            self.imageEdgeInsets = imageEdgeInsets;
            self.titleEdgeInsets = titleEdgeInsets;
            self.contentEdgeInsets = contentEdgeInsets;
         
        CGFloat buttonWidth = MAX(imageWidth, labelWidth);
        CGFloat buttonHeight = imageHeight + labelHeight + spacing;
        return CGSizeMake(buttonWidth, buttonHeight);
}

- (void)setImage:(UIImage *)image imageHL:(UIImage *)imageHL {
    [self setImage:image forState:UIControlStateNormal];
    [self setImage:imageHL forState:UIControlStateHighlighted];
}

static char topNameKey;
static char rightNameKey;
static char bottomNameKey;
static char leftNameKey;

- (void)setEnlargeEdgeWithTop:(CGFloat)top right:(CGFloat)right bottom:(CGFloat)bottom left:(CGFloat)left
{
    objc_setAssociatedObject(self, &topNameKey, [NSNumber numberWithFloat:top], OBJC_ASSOCIATION_COPY_NONATOMIC);
    objc_setAssociatedObject(self, &rightNameKey, [NSNumber numberWithFloat:right], OBJC_ASSOCIATION_COPY_NONATOMIC);
    objc_setAssociatedObject(self, &bottomNameKey, [NSNumber numberWithFloat:bottom], OBJC_ASSOCIATION_COPY_NONATOMIC);
    objc_setAssociatedObject(self, &leftNameKey, [NSNumber numberWithFloat:left], OBJC_ASSOCIATION_COPY_NONATOMIC);
}


- (CGRect)enlargedRect{
    NSNumber* topEdge    = objc_getAssociatedObject(self, &topNameKey);
    NSNumber* rightEdge  = objc_getAssociatedObject(self, &rightNameKey);
    NSNumber* bottomEdge = objc_getAssociatedObject(self, &bottomNameKey);
    NSNumber* leftEdge   = objc_getAssociatedObject(self, &leftNameKey);
    
    if (topEdge && rightEdge && bottomEdge && leftEdge){
        return CGRectMake(self.bounds.origin.x - leftEdge.floatValue,
                          self.bounds.origin.y - topEdge.floatValue,
                          self.bounds.size.width  + leftEdge.floatValue + rightEdge.floatValue,
                          self.bounds.size.height + topEdge.floatValue + bottomEdge.floatValue);
    } else{
        return self.bounds;
    }
}

@end
